﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data;

using System.Windows.Controls;
using System.Collections.Specialized;

namespace ManagmentWMS
{
    class Control
    {
        Firebird database = new Firebird();
        MainWindow mainWindow = new MainWindow();
        Session session = new Session();
        
        //DataTable dataTable = new DataTable();
        /*
        DataSet dsLocation = new DataSet();
        DataSet dsWarehouseman = new DataSet();
        
        DataSet dsDocTable = new DataSet();
        DataView dvDocTable = new DataView();
        
        DataSet dsDocLines = new DataSet();
        DataView dvDocLines = new DataView();
        */
        List<Location> locationList = new List<Location>();
        List<Warehouseman> warehousemanList = new List<Warehouseman>();
        List<DocumentLines> documentLines = new List<DocumentLines>();
        List<DocumentTable> documentTable = new List<DocumentTable>();        

        public Control(MainWindow mw)
        {
            this.mainWindow = mw;
        }

        public void Loaded()
        {
            mainWindow.tabControl.Items.Remove(mainWindow.tabWarehouseman);
            mainWindow.tabControl.Items.Remove(mainWindow.tabLocation);
            mainWindow.tabControl.Items.Remove(mainWindow.tabDocument);
            mainWindow.tbLogin.Focus();
            //this.Warning();
        }

        public void Login(string login, string passwd)
        {
            try
            {
                session.UserId = login;
                database.Login(login, passwd);
                mainWindow.tabControl.Items.Add(mainWindow.tabLocation);
                mainWindow.tabControl.Items.Add(mainWindow.tabWarehouseman);
                mainWindow.tabControl.Items.Add(mainWindow.tabDocument);
                mainWindow.tabControl.Items.Remove(mainWindow.tabLog);
                mainWindow.pbPasswd.Password = string.Empty;
                //mainWindow.cbDocument.Focus();
                this.InitLocation();
                this.InitWarehouseman();
                this.InitDocTable();
                this.InitDocLines(0);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Błąd formatki", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Logout()
        {
            try
            {
                database.Logout();
                if (!mainWindow.tabControl.Items.Contains(mainWindow.tabLog))
                    mainWindow.tabControl.Items.Add(mainWindow.tabLog);
                if (mainWindow.tabControl.Items.Contains(mainWindow.tabWarehouseman))
                    mainWindow.tabControl.Items.Remove(mainWindow.tabWarehouseman);
                if (mainWindow.tabControl.Items.Contains(mainWindow.tabLocation))
                    mainWindow.tabControl.Items.Remove(mainWindow.tabLocation);
                if (mainWindow.tabControl.Items.Contains(mainWindow.tabControl))
                    mainWindow.tabControl.Items.Remove(mainWindow.tabControl);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Błąd logowania do bazy danych", MessageBoxButtons.OK, MessageBoxIcon.Error);
                mainWindow.Focus();
            }
        }

        public void FindLocation()
        {
            /*
            string item = mainWindow.tbItemId.Text;
            string location = mainWindow.tbLocation.Text;//.Replace("-",string.Empty);
            //string query = string.Format("SELECT p.LOCATIONID, p.ITEMID FROM KASY_GET_LOCATION('{0}', '{1}') p",location,item);
            dvLocation.RowFilter = string.Format("itemid like '{0}*' and locationid like '{1}*'", item, location);
            dvLocation.RowStateFilter = DataViewRowState.CurrentRows;
            mainWindow.dataGridLocation.ItemsSource = dvLocation;
            if (mainWindow.dataGridLocation.Columns.Count > 0)
            {
                mainWindow.dataGridLocation.Columns[0].Header = "Lokalizacja";
                mainWindow.dataGridLocation.Columns[1].Header = "Model";
            }
             */ 
        }

        private void InitLocation()
        {
            string query = "SELECT p.LOCATIONID, p.ITEMID FROM KASY_GET_LOCATION('', '') p";
            mainWindow.dataGridLocation.ItemsSource = LocationList(database.GetData(query, "Location"));
            if(mainWindow.dataGridLocation.Columns.Count>0)
            {
                mainWindow.dataGridLocation.Columns[0].Header = "Lokalizacja";
                mainWindow.dataGridLocation.Columns[1].Header = "Model";
            }
        }

        private void InitWarehouseman()
        {
            string query = "SELECT p.LOGIN, p.NAME, p.ISACTIVE FROM KASY_GET_WAREHOUSEMAN('') p";
            mainWindow.dataGridWarehouseman.ItemsSource = WarehousemanList(database.GetData(query, "Warehouseman"));
            if (mainWindow.dataGridWarehouseman.Columns.Count > 0)
            {
                mainWindow.dataGridWarehouseman.Columns[0].Header = "Login";
                mainWindow.dataGridWarehouseman.Columns[1].Header = "Imię i Nazwisko";
                mainWindow.dataGridWarehouseman.Columns[2].Header = "Aktywność";
            }
        }

        private void InitDocTable()
        {
            string query = string.Format("SELECT first 50 p.DOCUMENT_ID, p.DOCUMENT_NAME, p.FROMWAREHOUSE, p.TOWAREHOUSE, p.DOCUMENT_TYPE, p.ISUSE, p.ISCLOSED, p.USER_USE, p.USER_CLOSED, p.CREATE_DATE FROM KASY_GET_DOC_TABLE('{0}') p",string.Empty);
            mainWindow.dataGridDocTable.ItemsSource = DocumentTable(database.GetData(query, "DocumentTable"));
            if (mainWindow.dataGridDocTable.Columns.Count > 0)
            {
                /*
                mainWindow.dataGridDocTable.Columns[0].Header = "Login";
                mainWindow.dataGridDocTable.Columns[1].Header = "Imię i Nazwisko";
                mainWindow.dataGridDocTable.Columns[2].Header = "Aktywność";
                */ 
            }            
        }

        private void InitDocLines(int documentId)
        {
            string query = string.Format("SELECT p.NAME, p.INDEKS, p.QTY, p.QTY_SCANNED, p.BOX_NUMBER FROM KASY_GET_POSITIONS({0}) p",0);
            mainWindow.dataGridDocLines.ItemsSource = DocumentLines(database.GetData(query, "DocumentLines"));
            if (mainWindow.dataGridDocLines.Columns.Count > 0)
            {                
                mainWindow.dataGridDocLines.Columns[0].Header = "Login";
                mainWindow.dataGridDocLines.Columns[1].Header = "Imię i Nazwisko";
                mainWindow.dataGridDocLines.Columns[2].Header = "Aktywność";                 
            }
        }

        public void Test(string value)
        {
            //mainWindow.label3.Content = (mainWindow.dataGridWarehouseman.SelectedItem as DataRowView).Row["Login"].ToString();
            //mainWindow.label3.Content = (mainWindow.dataGridWarehouseman.SelectedItem as DataRowView).Row[0].ToString();
            //DataRowView  drv = mainWindow.dataGridDocTable.SelectedItem as DataRowView;
            MessageBox.Show((mainWindow.dataGridWarehouseman.CurrentItem as DataRowView).Row[0].ToString());
            

        }

        private List<Location> LocationList(DataSet dataset)
        {
            DataView dv = new DataView(dataset.Tables[0]);            
            foreach(DataRowView dr in dv)
            {
                Location l = new Location();
                l.Indeks = dr[1].ToString();
                l.LocationId = dr[0].ToString();
                locationList.Add(l);
            }
            return locationList;
        }

        private List<Warehouseman> WarehousemanList(DataSet dataset)
        {
            DataView dv = new DataView(dataset.Tables[0]);
            bool value = false;
            foreach (DataRowView dr in dv)
            {
                Warehouseman w = new Warehouseman();
                w.Id    = dr[0].ToString();
                w.Name  = dr[1].ToString();
                if (dr[2].ToString()=="T")
                    value = true;
                else
                    value = false;
                w.IsActive = value;
                warehousemanList.Add(w);
            }
            return warehousemanList;
        }

        private List<DocumentLines> DocumentLines(DataSet dataset)
        {
            DataView dv = new DataView(dataset.Tables[0]);            
            foreach (DataRowView dr in dv)
            {
                DocumentLines dl = new DocumentLines();
                //p.NAME, p.INDEKS, p.QTY, p.QTY_SCANNED, p.BOX_NUMBER FROM
                dl.ModelName = dr[0].ToString();
                dl.Indeks = dr[1].ToString();                
                dl.Quantity = (int)(dr[2]);
                dl.QuantityScanned = (int)dr[3];
                dl.BoxNumber = (int)dr[4];                
                documentLines.Add(dl);
            }
            return documentLines;
        }

        private List<DocumentTable> DocumentTable(DataSet dataset)
        {
            DataView dv = new DataView(dataset.Tables[0]);
            foreach (DataRowView dr in dv)
            {
                DocumentTable dt = new DocumentTable();
                dt.Date = new DateTime();
                dt.DocumentId = dr[0].ToString();
                dt.DocumentName = dr[1].ToString();
                dt.From = dr[2].ToString();
                dt.To = dr[3].ToString();
                dt.DocumentType = dr[4].ToString();
                dt.IsClose = false;
                dt.IsUse = false;
                dt.UserClose = dr[7].ToString();
                dt.UserUse = dr[8].ToString();
                documentTable.Add(dt);
            }
            return documentTable;
        }
    }
}

